using System.Windows;
using System.Windows.Threading;

namespace CrackNET.InjectedViewer
{
    public delegate void DelayedHandler();

    public class DelayedCall
    {
        #region Fields

        private DelayedHandler _handler;
        private DispatcherPriority _priority;
        private bool _queued;
        
        #endregion

        #region Constructors

        public DelayedCall(DelayedHandler handler, DispatcherPriority priority)
        {
            _handler = handler;
            _priority = priority;
        }

        #endregion

        #region Methods

        #region Public

        public void Enqueue()
        {
            if (!_queued)
            {
                _queued = true;
                Application.Current.Dispatcher.BeginInvoke(_priority, new DispatcherOperationCallback(Process), null);
            }
        }

        #endregion

        #region Private

        private object Process(object arg)
        {
            _queued = false;
            _handler();
            return null;
        }

        #endregion

        #endregion
    }
}
